
// Copyright (C) 2007 Richard Uhler
// This file is part of Esiply.
//
// Esiply is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, Version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// This file contains the implementation for all the Boolean standard library.

// Boolean is both a function and a constructor, so we need to create
// a janus function for that.
Esiply.set_attributes(this, "Boolean", Esiply.DONT_ENUM);
Boolean = Esiply.create_janus_function(

// Ecma 15.6.1.1 Boolean(value)
// Retuns a boolean value (not a Boolean object) computed by ToBoolean(value)
function(value)
{
    // ! happens to call toBoolean. We just have to call it twice to get the
    // right value.
    return !!value;
},

// Ecma 15.6.2.1 new Boolean(value)
// [[prototype]] is set to the boolean prototype
// [[class]] is set to "Boolean"
// [[value]] is set to toBoolean(value)
function(value)
{
    var obj = new Object();
    Esiply.set_prototype(obj, Boolean.prototype);
    Esiply.set_class(obj, "Boolean");
    Esiply.set_value(obj, Boolean(value));
    return obj;
});

// Ecma 15.6.3.1 Boolean.prototype
// The initial value of Boolean.prototype is the Boolean prototype object
// This property has the attributes {DontEnum, DontDelete, ReadOnly}
Boolean.prototype = Esiply.boolean_prototype;
Esiply.set_attributes(Boolean, "prototype",
        Esiply.DONT_ENUM | Esiply.DONT_DELETE | Esiply.READ_ONLY);

// Ecma 15.6.4.1 Boolean.prototype.constructor
// The initial value of Boolean.prototype.constructor is the built-in
// Boolean constructor
Esiply.set_attributes(Boolean.prototype, "constructor", Esiply.DONT_ENUM);
Boolean.prototype.constructor = Boolean;

// Ecma 15.6.4.2 Boolean.prototype.toString()
// If this boolean value is true, then the string "true" is returned.
// Otherwise this boolean value must be false, and the string "false" is
// returned. This function throws a TypeError if the this object isn't
// a Boolean object.
Esiply.set_attributes(Boolean.prototype, "toString", Esiply.DONT_ENUM);
Boolean.prototype.toString = function()
{
    if (Esiply.get_class(this) != "Boolean")
    {
        throw new TypeError("Boolean.prototype.toString called on a non-boolean");
    }
    return this.valueOf() ? "true" : "false";
};

// Ecma 15.6.4.3 Boolean.prototype.valueOf()
// Returns this boolean value. It's not generic.
Boolean.prototype.valueOf = function()
{
    if (Esiply.get_class(this) != "Boolean")
    {
        throw new TypeError("Boolean.prototype.valueOf called on a non-boolean");
    }
    return Esiply.get_value(this);
};
